package com.taikang.controller;

import com.taikang.service.PushService;
import com.taikang.util.ResponseData;
import com.taikang.util.Status;
import com.taikang.util.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by zhai on 16/5/14.
 */
@Controller
public class PushController {

    private PushService pushService;

    @Autowired
    public void setPushService(PushService pushService) {
        this.pushService = pushService;
    }

    @RequestMapping("/push/list")
    @ResponseBody
    public Object getPushList(@RequestParam(value = "productId", required = false) Integer productId,
                              @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                              @RequestParam(value = "size", required = false, defaultValue = "3") Integer size) {
        ResponseData responseData = new ResponseData();
        Object o = pushService.getPushList(productId, page, size);
        if(o instanceof Pagination) {
            Pagination pagination = (Pagination) o;
            if (null == pagination || pagination.getDataList().size() == 0) {
                responseData.setMsg("暂无数据");
                responseData.setStatus(Status.error);
                return responseData;
            }
        }
        responseData.setData(o);
        responseData.setStatus(Status.success);
        return responseData;
    }
    @RequestMapping("/push/user")
    @ResponseBody
    public Object getPushUserList(@RequestParam(value = "employeeId") Integer employeeId,
                                  @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                  @RequestParam(value = "size", required = false, defaultValue = "3") Integer size){
        ResponseData responseData = new ResponseData();
        Object o = pushService.getPushUserList(employeeId,page,size);
        if(o instanceof Pagination) {
            Pagination pagination = (Pagination) o;
            if (null == pagination || pagination.getDataList().size() == 0) {
                responseData.setMsg("暂无数据");
                responseData.setStatus(Status.error);
                return responseData;
            }
        }
        responseData.setData(o);
        responseData.setStatus(Status.success);
        return responseData;
    }
    @RequestMapping("/push/add")
    @ResponseBody
    public Object addMessage(@RequestParam(value = "userId") Integer userId,
                             @RequestParam(value = "employeeId") Integer employeeId,
                             @RequestParam(value = "name") String name){
        ResponseData responseData = new ResponseData();
        boolean flag = false;
        flag = pushService.addMessage(userId, employeeId, name);
        if(flag){
            responseData.setStatus(Status.success);
            responseData.setMsg("写入成功");
        }else {
            responseData.setStatus(Status.error);
            responseData.setMsg("写入失败,请稍后再试");
        }
        return responseData;
    }
}
